package stage;

/**
 *
 * @author Matteo Cuccato
 * @author Nicola Girardi
 */
import java.io.*;
import java.util.Vector;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Coordinate;

public class salvaRisultati {

    public static void createJMLResult(Vector v){

        String localdir = System.getProperty("user.home");
        String path = localdir+"/Dropbox/stage/risultati_join.jml";
        try{
            
            GeometryIntersection gi;
            Geometry g;

            File file = new File(path);

            FileOutputStream fos = new FileOutputStream(file);
            PrintStream out = new PrintStream(fos);
            char virgolette = '"';
            char virgola = ',';

            out.println("<?xml version='1.0' encoding='UTF-8'?>");
            out.println("<JCSDataFile xmlns:gml=\"http://www.opengis.net/gml\" xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\">");
            out.println("<JCSGMLInputTemplate>");
            out.println("<CollectionElement>featureCollection</CollectionElement> ");
            out.println("<FeatureElement>feature</FeatureElement>");
            out.println("<GeometryElement>geometry</GeometryElement>");
            out.println("<ColumnDefinitions>");
            out.println("<column>");
            out.println("<name>FERROVIE_</name>");
            out.println("<type>INTEGER</type>");
            out.println("<valueElement elementName=\"property\" attributeName=\"name\" attributeValue=\"FERROVIE_\"/>");
            out.println("<valueLocation position=\"body\"/>");
            out.println("</column>");
            out.println("<column>");
            out.println("<name>INTERS_ID</name>");
            out.println("<type>INTEGER</type>");
            out.println("<valueElement elementName=\"property\" attributeName=\"name\" attributeValue=\"INTERS_ID\"/>");
            out.println("<valueLocation position=\"body\"/>");
            out.println("</column>");
            out.println("</ColumnDefinitions>");
            out.println("</JCSGMLInputTemplate>");
            out.println("");
            out.println("<featureCollection>");

            for (int i = 0; i < v.size(); i++ ){
                scriviTagIniziali(out);

                gi = (GeometryIntersection)v.elementAt(i);
                g = gi.getGeometry();
                Coordinate[] coord = g.getCoordinates();

                for( int j = 0; j < coord.length; j++ ){
                    out.print("" + coord[j].x);
                    out.print(virgola);
                    out.print("" + coord[j].y);
                    out.println("");
                }


                scriviTagFinaliResult(out,gi);
            }

            out.println("</featureCollection>");
            out.println("</JCSDataFile>");

            out.close();
            fos.close();

        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void scriviTagIniziali(PrintStream out) {
        out.println("<feature>");
        out.println("<geometry>");
        out.println("<gml:LineString>");
        out.println("<gml:coordinates>");
    }

    public static void scriviTagFinaliResult(PrintStream out, GeometryIntersection gi) {
        out.println("</gml:coordinates>");
        out.println("</gml:LineString>");
        out.println("</geometry>");
        out.println("<property name=\"FERROVIE_\">"+gi.getId()+"</property>");
        out.println("<property name=\"INTERS_ID\">"+gi.getIntersection_id()+"</property>");
        out.println("</feature>");
    }
}
